#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *file_open(const char *path, const char *mode);

int file_close(FILE **file);

typedef struct Elenment_data
{
    char name[20];
    int id;
    int sco;

} Elenment_data;

Elenment_data data[5] = {
    {"张三", 1, 100},
    {"李四", 2, 90},
    {"王五", 3, 80},
    {"赵六", 4, 70},
    {"钱七", 5, 60},

};

int main(int argc, const char *argv[])
{
    FILE *fp = file_open("data.txt", "r+");

    for (size_t i = 0; i < 5; i++)
    {
        fprintf(fp, "姓名 %s\t学号%d\t成绩%d\n", data[i].name, data[i].id, data[i].sco);
    }

    file_close(&fp);

    fp = file_open("data.txt", "r+");

    Elenment_data data1[5];

    for (size_t i = 0; i < 5; i++)
    {
        fscanf(fp, "姓名 %s\t学号%d\t成绩%d\n", data1[i].name, &data1[i].id, &data1[i].sco);
    }

    for (size_t i = 0; i < 5; i++)
    {
        fprintf(stdout , "姓名 %s\t学号%d\t成绩%d\n", data1[i].name, data1[i].id, data1[i].sco);
    }
    file_close(&fp);

    return 0;
}

FILE *file_open(const char *path, const char *mode)
{
    FILE *fp = fopen(path, mode);
    if (NULL == fp)
    {
        printf("文件创建失败\n");
        exit(-1);
    }
    return fp;
}

int file_close(FILE **fp)
{
    fclose(*fp);
    *fp = NULL;
}